package com.ruoyi.elebook.service.impl;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.elebook.domain.BookClass;
import com.ruoyi.elebook.mapper.BookClassMapper;
import com.ruoyi.elebook.service.IBookClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
public class BookClassServiceImpl implements IBookClassService
{
    @Autowired
    private BookClassMapper bookClassMapper;

    /**
     * 查询分类
     *
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public BookClass selectBookClassById(Long id)
    {
        return bookClassMapper.selectBookClassById(id);
    }

    /**
     * 查询分类列表
     *
     * @param bookClass 分类
     * @return 分类
     */
    @Override
    public List<BookClass> selectBookClassList(BookClass bookClass)
    {
        return bookClassMapper.selectBookClassList(bookClass);
    }

    /**
     * 新增分类
     *
     * @param bookClass 分类
     * @return 结果
     */
    @Override
    public int insertBookClass(BookClass bookClass)
    {
        BookClass info = bookClassMapper.selectBookClassById(bookClass.getParentId());
        bookClass.setAncestors(info.getAncestors() + "," + bookClass.getParentId());
        bookClass.setCreateTime(DateUtils.getNowDate());
        return bookClassMapper.insertBookClass(bookClass);
    }

    /**
     * 修改分类
     *
     * @param bookClass 分类
     * @return 结果
     */
    @Override
    public int updateBookClass(BookClass bookClass)
    {
        bookClass.setUpdateTime(DateUtils.getNowDate());
        return bookClassMapper.updateBookClass(bookClass);
    }

    /**
     * 批量删除分类
     *
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteBookClassByIds(Long[] ids)
    {
        return bookClassMapper.deleteBookClassByIds(ids);
    }

    /**
     * 删除分类信息
     *
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteBookClassById(Long id)
    {
        return bookClassMapper.deleteBookClassById(id);
    }

    /**
     * 校验分类名称是否唯一
     *
     * @param bookClass 部门信息
     * @return 结果
     */
    @Override
    public boolean checkBookClassNameUnique(BookClass bookClass)
    {
        Long deptId = StringUtils.isNull(bookClass.getId()) ? -1L : bookClass.getId();
        BookClass info = bookClassMapper.checkBookClassNameUnique(bookClass.getName(), bookClass.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验分类代号是否唯一
     *
     * @param bookClass 部门信息
     * @return 结果
     */
    @Override
    public boolean checkBookClassCodeUnique(BookClass bookClass)
    {
        Long deptId = StringUtils.isNull(bookClass.getId()) ? -1L : bookClass.getId();
        BookClass info = bookClassMapper.checkBookClassCodeUnique(bookClass.getCode(), bookClass.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 是否存在子节点
     *
     * @param id 分类ID
     * @return 结果
     */
    @Override
    public boolean hasChildByBookClassId(Long id)
    {
        int result = bookClassMapper.hasChildByBookClassId(id);
        return result > 0;
    }
}
